package serverlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import connectDB.CardDB;
import connectDB.FaqDB;
import connectDB.IDB;
import connectDB.PacketDB;
import entities.Cartentities;
import entities.Faqentities;
import entities.Packetentities;

/**
 * Servlet implementation class Cardcontroller
 */
@WebServlet("/Cardcontroller")
public class Cardcontroller extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Cardcontroller() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String link = "/Manager/cardlist.jsp";
		IDB<Cartentities, Boolean, String> pack = new CardDB();
		List<Cartentities> list= new ArrayList<Cartentities>();
		list = pack.findAll();
		request.setAttribute("listcard", list);
		String url = link;
		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws IOException {


		String action = request.getParameter("btaction");
		
		if(action.equals("edit")){
			this.doedit(request, response);
		}
		
	}

	private void doedit(HttpServletRequest request, HttpServletResponse response) throws IOException {


		try {
    		IDB<Cartentities, Boolean, String> acc = new CardDB();
			int id = Integer.parseInt(request.getParameter("id"));
			String landName = request.getParameter("name");
			Cartentities land = new Cartentities(id, landName);
			boolean result = acc.update(land);
			String url = null;
			if(result){
				HttpSession session = request.getSession(true);
				url = "/Manager/cardlist.jsp";
			}
			RequestDispatcher rd = request.getRequestDispatcher(url);
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
